package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.VendorTagMapper;
import org.linlinjava.litemall.db.domain.VendorGoodsTag;
import org.linlinjava.litemall.db.domain.VendorTag;
import org.linlinjava.litemall.db.domain.VendorTagExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class VendorTagService {
    @Resource
    private VendorTagMapper vendorTagMapper;

    /**
     * 根据当前商户的标签列表
     * @return
     */
    public List<VendorTag> queryTags(String name, Integer page, Integer limit, String sort, String order) {
        VendorTagExample example = new VendorTagExample();
        VendorTagExample.Criteria criteria = example.createCriteria();

        if (!StringUtils.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return vendorTagMapper.selectByExample(example);
    }

    public List<VendorTag> findByVendorId(Long vendorId) {
        VendorTagExample example = new VendorTagExample();
        VendorTagExample.Criteria criteria = example.createCriteria();
        criteria.andVendorIdEqualTo(vendorId);
        return vendorTagMapper.selectByExample(example);
    }

    /**
     * 添加标签
     * @param tag
     * @return
     */
    public boolean addTag(VendorTag tag) {
        return vendorTagMapper.insertSelective(tag) > 0;
    }

    /**
     * 删除标签
     * @param tagId
     * @return
     */
    public boolean deleteById(long tagId) {
        VendorTagExample example = new VendorTagExample();
        example.or().andIdEqualTo(tagId);
        return vendorTagMapper.deleteByPrimaryKey(tagId) > 0;
    }

    public VendorTag findById(Long id) {
        return vendorTagMapper.selectByPrimaryKey(id);
    }

    /**
     * 更新标签
     * @param tag
     * @return
     */
    public int updateById(VendorTag tag) {
        tag.setUpdateTime(LocalDateTime.now());
        return vendorTagMapper.updateByPrimaryKeySelective(tag);
    }
}
